package com.example.newland.application82;

import android.util.Log;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class HttpUrl {

    public static String post(String url,String pamer,String token){

        HttpURLConnection conn = null;
        PrintWriter printWriter = null;
        String cn = null;
        BufferedReader bufferedReader = null;
        StringBuffer stringBuffer = new StringBuffer();

        try {
            URL u = new URL(url);
            conn = (HttpURLConnection)u.openConnection();
            conn.setDoInput(true);
            conn.setDoOutput(true);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("content-Type","application/json");
            if(token != null){
                conn.setRequestProperty("AccessToken",token);
            }

            //发送数据
            printWriter = new PrintWriter(new OutputStreamWriter(conn.getOutputStream(), "UTF-8"));
            printWriter.print(pamer);
            printWriter.flush();

            //数据响应
            bufferedReader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            while((cn = bufferedReader.readLine())!= null){
                stringBuffer.append(cn);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if(printWriter != null){
                printWriter.close();
            }
            if(bufferedReader != null){
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            conn.disconnect();
        }

        return stringBuffer.toString();
    }

    public static String get(String url,String token){

        HttpURLConnection conn = null;
        String cn = null;
        BufferedReader bufferedReader = null;
        StringBuffer stringBuffer = new StringBuffer();

        try {
            URL u = new URL(url);
            conn = (HttpURLConnection)u.openConnection();
            if(token != null){
                conn.setRequestProperty("AccessToken",token);
            }
            if(conn.getResponseCode() == 200){
                //数据响应
                bufferedReader = new BufferedReader(new InputStreamReader(conn.getInputStream(),"utf-8"));
                while((cn = bufferedReader.readLine())!= null){
                    stringBuffer.append(cn);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if(bufferedReader != null){
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            conn.disconnect();
        }

        return stringBuffer.toString();
    }

}
